<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>天天生鲜-商品详情</title>
	<link rel="stylesheet" type="text/css" href="${path}/static/front/css/reset.css">
	<link rel="stylesheet" type="text/css" href="${path}/static/front/css/main.css">

</head>
<body>
	<div class="header_con">
		<div class="header">
			<div class="welcome fl">欢迎来到天天生鲜!</div>
			<div class="fr">
				<div class="login_info fl">
					欢迎您：<em>张 山</em>
				</div>
				<div class="login_btn fl">
					<a href="login.html">登录</a>
					<span>|</span>
					<a href="register.html">注册</a>
				</div>
				<div class="user_link fl">
					<span>|</span>
					<a href="user_center_info.html">用户中心</a>
					<span>|</span>
					<a href="cart.html">我的购物车</a>
					<span>|</span>
					<a href="user_center_order.html">我的订单</a>
				</div>
			</div>
		</div>		
	</div>

	<div class="search_bar clearfix">
		<a href="index.html" class="logo fl"><img src="${path}/static/front/images/logo.png"></a>
		<div class="search_con fl">
			<input type="text" class="input_text fl" name="" placeholder="搜索商品">
			<input type="button" class="input_btn fr" name="" value="搜索">
		</div>
		<div class="guest_cart fr">
			<a href="cart.html" class="cart_name fl">我的购物车</a>
			<div class="goods_count fl" id="show_count">3</div>
		</div>
	</div>

	<div class="navbar_con">
		<div class="navbar clearfix">
			<div class="subnav_con fl">
				<h1>全部商品分类</h1>	
				<span></span>			
				<ul class="subnav">
					<li><a href="#" class="fruit">新鲜水果</a></li>
					<li><a href="#" class="seafood">海鲜水产</a></li>
					<li><a href="#" class="meet">猪牛羊肉</a></li>
					<li><a href="#" class="egg">禽类蛋品</a></li>
					<li><a href="#" class="vegetables">新鲜蔬菜</a></li>
					<li><a href="#" class="ice">速冻食品</a></li>
				</ul>
			</div>
			<ul class="navlist fl">
				<li><a href="">首页</a></li>
				<li class="interval">|</li>
				<li><a href="">手机生鲜</a></li>
				<li class="interval">|</li>
				<li><a href="">抽奖</a></li>
			</ul>
		</div>
	</div>

	<div class="breadcrumb">
		<a href="#">全部分类</a>
		<span>></span>
		<a href="#">新鲜水果</a>
		<span>></span>
		<a href="#">商品详情</a>
	</div>

	<div class="goods_detail_con clearfix">
		<div class="goods_detail_pic fl"><img src="/pic/${product.mainImage}"></div>

		<div class="goods_detail_list fr">
			<h3>${product.name}</h3>
			<p>${product.subtitle}</p>
			<div class="prize_bar">
				<span class="show_pirze">¥<em>${product.price}</em></span>
				<span class="show_unit">单  位：500g</span>
			</div>
			<div class="goods_num clearfix">
				<div class="num_name fl">数 量：</div>
				<div class="num_add fl">
					<input id="num" type="text" class="num_show fl" value="1">
					<a href="javascript:;" class="add fr">+</a>
					<a href="javascript:;" class="minus fr">-</a>	
				</div> 
			</div>
			<div class="total">总价：<em>16.80元</em></div>
			<div class="operate_btn">
				<a href="javascript:;" class="buy_btn">立即购买</a>
				<a href="javascript:;" class="add_cart" id="add_cart" onclick="insertProductToCart()">加入购物车</a>				
			</div>
		</div>
	</div>

	<div class="main_wrap clearfix">
		<div class="l_wrap fl clearfix">
			<div class="new_goods">
				<h3>新品推荐</h3>
				<ul>
					<li>
						<a href="#"><img src="${path}/static/front/images/goods/goods001.jpg"></a>
						<h4><a href="#">进口柠檬</a></h4>
						<div class="prize">￥3.90</div>
					</li>
					<li>
						<a href="#"><img src="${path}/static/front/images/goods/goods002.jpg"></a>
						<h4><a href="#">玫瑰香葡萄</a></h4>
						<div class="prize">￥16.80</div>
					</li>
				</ul>
			</div>
		</div>

		<div class="r_wrap fr clearfix">
			<ul class="detail_tab clearfix">
				<li class="active">商品介绍</li>
				<li>评论</li>
			</ul>

			<div class="tab_content">
				<dl>
					<dt>商品详情：</dt>
					<dd>${product.detail}</dd>
				</dl>
			</div>

		</div>
	</div>

	<div class="footer">
		<div class="foot_link">
			<a href="#">关于我们</a>
			<span>|</span>
			<a href="#">联系我们</a>
			<span>|</span>
			<a href="#">招聘人才</a>
			<span>|</span>
			<a href="#">友情链接</a>		
		</div>
		<p>CopyRight © 2016 北京天天生鲜信息技术有限公司 All Rights Reserved</p>
		<p>电话：010-****888    京ICP备*******8号</p>
	</div>
	<div class="add_jump"></div>

	<div id="loginForm" class="login_form fr" style="display:none">
			<div class="login_title clearfix">
				<h1>用户登录</h1>
				<a href="#">立即注册</a>
			</div>
			<div class="form_input">
				<form id="login_form" onsubmit="return false" method="post">
					<input type="text" id="name" name="name" class="name" placeholder="请输入用户名">
					<div class="user_error">输入错误</div>
					<input type="password" id="password" name="password" class="password" placeholder="请输入密码">
					<div class="pwd_error">输入错误</div>
					<div class="more_input clearfix">
						<input type="checkbox" name="">
						<label>记住用户名</label>
						<a href="#">忘记密码</a>
					</div>
					<input type="submit" onclick="submitForm()" name="" value="登录" class="input_submit">
				</form>
			</div>
	</div>
	
	
	<script src="${path}/static/lib/jquery-1.11.3.js"></script>
	<script src="${path}/static/lib/mylayer.js"></script>
	<script src="${path}/static/lib/util.js"></script>
	<script src="${path}/static/lib/layui/layui.js"></script>
	
	
	<script type="text/javascript">
		layui.use(['layer'], function(){
			  // 只加载LayUI的table模块
			  var layer = layui.layer;
		});
		
		function insertProductToCart(){
			// session是一个域对象，${user}能取出保存到session中的用户登录信息
			// User user = (User)session.getAttribute("user");
			var user = "${user}";
			if(user == ""){// 这个用户没有登录，弹出登录框
				layer.open({
					type:1,
					title:"登录",
					offset:"50px",
					area:["450px","500px"],
					content:$("#loginForm")
				});
			}else{// 这个用户已经登录，将商品插入购物车，成功后跳转到购物车界面
				$.ajax({
					url:"${path}/cart/insert.shtml",
					data:{"productId":"${product.id}", "num":$("#num").val()},
					type:"post",
					dataType:"json",
					success:function(jsonObj){
						if(jsonObj.code == util.SUCCESS){
							mylayer.successMsgUrl(jsonObj.msg, "${path}/cart/getCartListPage.shtml");
						}else{
							mylayer.errorMsg(jsonObj.msg);
						}
					}
				});
			}
		}
		
		function submitForm(){
			
			console.log("submitForm");
    		var name = $("#name").val();
    		var password = $("#password").val();
    		
    		if(util.isEmpty(name)){
    			mylayer.errorMsg("用户名不能为空");
    			return;
    		}
    		
    		var pattern = /^\w{4,8}$/;
    		if(!pattern.test(name)){
    			mylayer.errorMsg("用户名非法，请输入4-8位数字或字母");
    			return;
    		}
    		
    		if(util.isEmpty(password)){
    			mylayer.errorMsg("密码不能为空");
    			return;
    		}
    		
    		var pattern = /^\w{3}$/;
    		if(!pattern.test(password)){
    			mylayer.errorMsg("密码格式非法，请输入3位数字或字母");
    			return;
    		}

    		$.ajax({
    			url : "${path}/user/frontLogin.shtml",
    			data : $("#login_form").serialize(),
    			type : "post",
    			dataType : "json",
    			success : function(jsonObj){
    				console.log(jsonObj);
    				console.log($("#login_form").serialize());
    				if(jsonObj.code == util.SUCCESS){
    					mylayer.successMsg(jsonObj.msg);
						//当你在iframe页面关闭自身时
						var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
// 						setTimeout(function(){
// 							parent.layer.close(index); //再执行关闭 
// 							// 刷新父页面
// 							window.parent.location.reload();
// 						},1000);
						
					$.ajax({
						url:"${path}/cart/insert.shtml",
						data:{"productId":"${product.id}", "num":$("#num").val()},
						type:"post",
						dataType:"json",
						success:function(jsonObj){
							if(jsonObj.code == util.SUCCESS){
								mylayer.successMsgUrl(jsonObj.msg, "${path}/cart/getCartListPage.shtml");
							}else{
								mylayer.errorMsg(jsonObj.msg);
							}
						}
					});
					
				} else {
						mylayer.errorMsg(jsonObj.msg);
					}
    			}
    		});
    	}
    </script>
	
</body>
</html>